#!/usr/bin/env python
# coding=utf-8


from PySide6.QtWidgets import QGridLayout, QPushButton

from interface.signal import SignalInterface


class BlockWidget(object):
    def __init__(self, layout: QGridLayout, signal: SignalInterface[QPushButton] = None):
        col = layout.columnCount()
        row = layout.rowCount()
        self.block = [[-1] * col for _ in range(row)]
        for i in range(row):
            for j in range(col):
                btn: QPushButton = layout.itemAtPosition(i, j).widget()
                # lambda for lazy binding, default should be set
                btn.clicked.connect(lambda _, btn=btn, row=i, col=j: self.change_state(btn, row, col))
                if signal is not None:
                    signal.connect_slot(btn)

    def reset(self):
        for i in range(len(self.block)):
            self.block[i] = [-1] * len(self.block[i])

    def change_state(self, btn: QPushButton, row: int, col: int):
        if btn.text() == "":
            btn.setText("●")
        else:
            btn.setText("")
        self.block[row][col] *= -1
